﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EHR.Ctrls.QueryDesign;
using EHR.Ctrls;
using System.Windows.Data;
using System.Collections.ObjectModel;
using CommunitySystem.Views;
using CommunitySystem.Assert;
using CommunitySystem;
using CommunitySystem.Ctrls;
using CommunitySystem.DataQueryService;

namespace EHR.Views
{
    public partial class CustomerQueryView : AsyncCtrl, IReturnListenerc
    {
        public NavMoveSwitch Nav { get; set; }
        private bool _initDataLoaded = false;
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private CustomerDesignView _customerDesignView;

        private ViewInfo SelectedViewInfo
        {
            get
            {
                if (lbViews.SelectedIndex == -1)
                {
                    return null;
                }
                ViewInfo v = ViewInfoList[lbViews.SelectedIndex];
                return v;
            }
        }

        public ObservableCollection<ViewInfo> ViewInfoList
        {
            get { return lbViews.ItemsSource as ObservableCollection<ViewInfo>; }
        }

        public CustomerQueryView()
        {
            InitializeComponent();
            Worker.IsBusy = true;
            DataQueryClient client = new DataQueryClient();
            client.GetQueryViewsCompleted += client_GetQueryViewsCompleted;
            client.GetQueryViewsAsync();
        }

        private void AsyncCtrl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public void QueryInitData()
        {
            if (_initDataLoaded)
            {
                return;
            }
            Worker.IsBusy = true;
            DataQueryClient client = new DataQueryClient();
            client.GetQueryViewsCompleted += client_GetQueryViewsCompleted;
            client.GetQueryViewsAsync();
        }

        void client_GetQueryViewsCompleted(object sender, GetQueryViewsCompletedEventArgs e)
        {
            Worker.IsBusy = false;
            ObservableCollection<ViewInfo> coll = new ObservableCollection<ViewInfo>(e.Result);
            lbViews.ItemsSource = coll;
            _initDataLoaded = true;
            if (coll.Count != 0)
            {
                lbViews.SelectedIndex = 0;
            }
        }

        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }

        private void lbViews_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbViews != null && lbViews.SelectedIndex != -1)
            {
                if (!dataQueryView.PageLoaded)
                {
                    dataQueryView.PageLoaded = true;
                }
                dataQueryView.Query(SelectedViewInfo);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            CustomerDesignView view = new CustomerDesignView() { Nav = Nav };
            _customerDesignView = view;
            this.IsEnabled = false;
            Nav.ShowNextPage("新建视图", view, this);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedViewInfo == null)
            {
                return;
            }
            CustomerDesignView view = new CustomerDesignView() { Nav = Nav, ViewInfo = SelectedViewInfo };
            _customerDesignView = view;
            this.IsEnabled = false;
            Nav.ShowNextPage("编辑视图", view, this);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedViewInfo == null)
            {
                return;
            }

            if (MessageBox.Show("确实要删除吗？", "提示", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            {
                return;
            }

            this.IsEnabled = false;
            DataQueryClient client = new DataQueryClient();
            client.DeleteViewCompleted += client_DeleteViewCompleted;
            client.DeleteViewAsync(SelectedViewInfo.ViewID);
        }

        void client_DeleteViewCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.IsEnabled = true;
            if (e.Error != null)
            {
                throw new Exception(e.Error.Message);
            }

            int i = lbViews.SelectedIndex;
            ViewInfoList.RemoveAt(i);
            if (ViewInfoList.Count == 0)
            {
                dataQueryView.Query(null);
            }
            else
            {
                if (i < ViewInfoList.Count)
                {
                    lbViews.SelectedIndex = i;
                }
                else
                {
                    lbViews.SelectedIndex = i - 1;
                }
            }
        }

        public void BeginReturn()
        {
            this.IsEnabled = true;
            if (_customerDesignView.IsSaved)
            {
                if (_customerDesignView.IsAdd)
                {
                    ViewInfo nvi = _customerDesignView.ViewInfo;
                    ViewInfoList.Add(nvi);
                    lbViews.SelectedIndex = ViewInfoList.Count - 1;
                    dataQueryView.Query(nvi);
                }
                else
                {
                    ViewInfo svi = SelectedViewInfo;
                    ViewInfo nvi = _customerDesignView.ViewInfo;
                    CopyViewInfo(nvi, svi);
                    dataQueryView.Query(nvi);
                }
            }
        }

        private void CopyViewInfo(ViewInfo source, ViewInfo target)
        {
            target.ViewID = source.ViewID;
            target.ViewName = source.ViewName;
            target.XML = source.XML;
        }
    }
}
